package cn.afactory.tygz.ui.sns.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.harwkin.nb.camera.BaseImgView;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.common.Gendar;
import cn.afactory.tygz.model.SnsUserInfo;
import cn.afactory.tygz.ui.base.utils.SPUtils;

/**
 *用户列表适配器
 */
public class UserListAdapter extends BaseAdapter {
    private List<SnsUserInfo> mList = new ArrayList<SnsUserInfo>();
    private Context mContext;

    public UserListAdapter(Context ctx, List<SnsUserInfo> list) {
        mList = list;
        mContext = ctx;
    }

    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return mList != null ? mList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        final SnsUserInfo snsUserInfo = mList.get(position);
        if(convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_user_list,null);

            holder.iv_user_avar = (ImageView)convertView.findViewById(R.id.iv_user_avar);
            holder.iv_user_gendar = (ImageView)convertView.findViewById(R.id.iv_user_gendar);
            holder.iv_user_vip = (ImageView)convertView.findViewById(R.id.iv_user_vip);

            holder.tv_user_name = (TextView)convertView.findViewById(R.id.tv_user_name);
            holder.tv_user_desc = (TextView)convertView.findViewById(R.id.tv_user_desc);

            holder.btn_attention = (TextView)convertView.findViewById(R.id.btn_attention);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }

        if (snsUserInfo.userIcon != null) {
            BaseImgView.loadimg(holder.iv_user_avar,
                    snsUserInfo.userIcon,
                    R.drawable.ic_pic_loading_default_small,
                    R.drawable.ic_pic_loading_default_small,
                    R.drawable.ic_pic_loading_default_small,
                    ImageScaleType.EXACTLY,
                    (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30),
                    (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30), 0);
        } else {
            holder.iv_user_avar.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_pic_loading_default_small));
        }

        if(SPUtils.isSelf(mContext,snsUserInfo.userId)){
            holder.btn_attention.setVisibility(View.GONE);
        }else {
            holder.btn_attention.setVisibility(View.VISIBLE);
            if (snsUserInfo.isAttentioned) {
                holder.btn_attention.setText(R.string.label_btn_cancel_attention);
            } else {
                holder.btn_attention.setText(R.string.label_btn_attention);
            }
        }

        holder.btn_attention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(snsUserInfo.isAttentioned){
                    //TODO取消关注
                    if (mOnItemClickListener == null) {
                        return;
                    }
                    mOnItemClickListener.onCancelAttentionClick((TextView) v, snsUserInfo);
                }else{
                    //TODO关注
                    if (mOnItemClickListener == null) {
                        return;
                    }
                    mOnItemClickListener.OnAttentionClick((TextView) v, snsUserInfo);
                }

            }
        });

        if(snsUserInfo.nickName != null){
            holder.tv_user_name.setText(snsUserInfo.nickName);
        }else{
            holder.tv_user_desc.setText("");
        }

        if(snsUserInfo.signature != null){
            holder.tv_user_desc.setText(snsUserInfo.signature);
        }else{
            holder.tv_user_desc.setText("");
        }

        if (snsUserInfo.sex > -1) {
            holder.iv_user_gendar.setVisibility(View.VISIBLE);
            if (snsUserInfo.sex == Gendar.FEMALE) {
                holder.iv_user_gendar.setImageDrawable(mContext.getResources().getDrawable(R.drawable.tag_female));
            } else if (snsUserInfo.sex == Gendar.MALE) {
                holder.iv_user_gendar.setImageDrawable(mContext.getResources().getDrawable(R.drawable.tag_male));
            } else {
                holder.iv_user_gendar.setImageDrawable(mContext.getResources().getDrawable(R.drawable.tag_female));
            }
        } else {
            holder.iv_user_gendar.setVisibility(View.GONE);
        }

        return convertView;
    }

    public void setData(List<SnsUserInfo> data) {
        this.mList = data;
        notifyDataSetChanged();
    }

    OnItemClickListener mOnItemClickListener;
    public void setOnItemClickListener(OnItemClickListener lsn){
        mOnItemClickListener = lsn;
    }

    public interface OnItemClickListener{
        void OnAttentionClick(TextView v,SnsUserInfo snsUserInfo);

        void onCancelAttentionClick(TextView v,SnsUserInfo snsUserInfo);
    }

    static class ViewHolder{
        public ImageView iv_user_avar;
        public ImageView iv_user_gendar;
        public ImageView iv_user_vip;
        public TextView tv_user_name;
        public TextView tv_user_desc;
        public TextView btn_attention;
    }
}